package insim;

import dataAccess.*;
import logging.*;
import robot.*;
import settings.*;
import userInterface.*;

import java.util.*;

/**
 * Main class which handles Insim execution.
 * @author BDS
 */
public class Main
{
	/**
	 * Static constructor.
	 */
	private Main() { }

	/**
	 * Main method.
	 * Displays the Insim UI.
	 * @param arguments Currently unsused.
	 */
	public static void main(String[] arguments)
	{
		new FrameMain().setVisible(true);
	}

	/**
	 * Sequentially executes all Commands for every Workflow defined in each Environment.
	 * TODO Set up a command pattern with {@link userInterface.panels.PanelMain} to update the JTree.
	 */
	public static void run()
	{
		try
		{
			Settings settings = LocalDataAccess.loadSettings();
			ExecutionRobot robot = new ExecutionRobot();
			
			List<SessionResult> sessionResults = new ArrayList<SessionResult>();
			for (Environment environment : settings.getEnvironments())
			{
				if (environment.getEnabled())
				{
					robot.loadEnvironment(environment);
					
					SessionResult sessionResult = environment.execute(robot);
					sessionResults.add(sessionResult);
					
					if (sessionResult.getSuccess())
					{
						Logger.logMessage(environment.getName() + ": Successful");
					}
					else
					{
						Logger.logMessage(environment.getName() + ": Failed");
					}
					
					if (sessionResult.getSuccess())
					{
						
					}
				}
			}
		}
		catch (Exception exception)
		{
			Logger.logMessage("General error");
			Logger.logException(exception);
		}
	}
}
